import { defineStore } from "pinia";
import axios from "axios";

export const useDateStore = defineStore("data", {
    state: () => ({
        //后端请求数据
        data: [],
        MvList: [],
        isPlaying: false,
        songName: '',
        length: 0,
        obj: {}
    }),
    actions: {
        // 获取数据
        async getData() {
            // 假设 fetchData()是一个返回 Promise 的函数
            const data = await fetchData();
            this.data = data;
        },
        async getMvList(cid) {
            await axios.get(`http://localhost:3000/getMvList/?cid=${cid}`)
                .then(res => {
                    this.MvList = res.data.list
                    this.length = res.data.list.classList.length
                    console.log(res.data.list);
                    console.log(res.data.list.classList.length, '111');
                })
        },
        async start(name, cid) {
            console.log('start', name, cid);
            axios
            .get(`http://localhost:3000/updateState/?name=${name}&id=${cid}`)
            .then((res) => {
                // console.log(res.data);
                this.obj = res.data.obj
            });
        },
        async end(name, cid) {
            console.log('end', name, cid);
            axios
                .get(`http://localhost:3000/updateStateN/?name=${name}&id=${cid}`)
                .then((res) => {
                    // console.log(res.data,.);
                    this.obj = res.data.obj
                });
        },
        play() {
            // 播放音乐逻辑
            let url = JSON.parse(localStorage.getItem('data')).obj.link
            let audio = new Audio(url);
            audio.play()
        },
        pause() {
            // 暂停音乐逻辑
            let url = JSON.parse(localStorage.getItem('data')).obj.link
            let audio = new Audio(url);
            audio.pause()
        },
        // 其他方法...
    },

    //持久化
    persist: {
        enabled: true, // 启用持久化
        strategies: [{ storage: localStorage }], // 持久化策略
    },
});

